package org.devocean.groupbuying.web.frontend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.UIManager;

import org.devocean.groupbuying.domain.Explored;
import org.devocean.groupbuying.service.ExploredService;
import org.devocean.groupbuying.service.ProductService;
import org.devocean.groupbuying.web.frontend.displaybeans.ExplorerBean;
import org.devocean.groupbuying.web.frontend.displaybeans.ExplorerDetailsBean;
import org.devocean.groupbuying.web.frontend.displaybeans.ProductBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = { "/{lang:[a-z]{2}}/clouds" })
// @RequestMapping(value={"", "/{lang:[a-z]{2}}"})
public class CloudsController {

	@Autowired
	private ExploredService exploredService;

	@Autowired
	private ProductService productService;

	@RequestMapping("explorer")
	public String index(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang, String order,
			String sort, Model model) {
		model.asMap().clear();
		String returnUrl = "#";
		if (sort == null) {
			returnUrl = "redirect:/" + lang + "/clouds/explorer/1/10";
		} else if (order == null) {
			model.addAttribute("order", "desc");
			returnUrl = "redirect:/" + lang + "/clouds/explorer/" + sort
					+ "/1/10";
		} else {
			if (!order.equals("asc") && !order.equals("desc")) {
				model.addAttribute("order", "desc");
			} else {
				model.addAttribute("order", order);
			}
			returnUrl = "redirect:/" + lang + "/clouds/explorer/" + sort + "/"
					+ "/1/10";
		}

		return returnUrl;
	}

	@RequestMapping("details/{id}")
	public String explorerDetails(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("id") Integer id) {

		map.put("detail",
				new ExplorerDetailsBean(exploredService.getExploredById(lang,
						id)));

		return "explorerDetails";
	}

	@RequestMapping("pdetails/{id}")
	public String productDetails(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("id") Integer id) {

		map.put("pDetail", new ProductBean(productService.getProductById(id)));

		return "productDetails";
	}

	@RequestMapping("explorer/{index}/{max}")
	public String paginate(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max) {
		long count = exploredService.countExploreds(lang);
		List<ExplorerBean> explorerBeans = new ArrayList<ExplorerBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}
		int start = (int) ((index * max) - max);
		for (Explored ex : exploredService
				.getExploredsEntries(lang, start, max)) {
			explorerBeans.add(new ExplorerBean(ex));
		}
		map.put("explorerBeans", explorerBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("order", "desc");
		map.put("sort", "all");
		map.put("path", "clouds/explorer/");
		return "explorer";
	}

	@RequestMapping("explorer/date/{index}/{max}")
	public String paginateByDate(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max, String order) {
		if (order == null) {
			order = "desc";
		}
		long count = exploredService.countExploreds(lang);
		List<ExplorerBean> explorerBeans = new ArrayList<ExplorerBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}
		int start = (int) ((index * max) - max);
		for (Explored ex : exploredService.getExploredsEntriesByDate(lang,
				start, max, order)) {
			explorerBeans.add(new ExplorerBean(ex));
		}
		map.put("explorerBeans", explorerBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("order", order);
		map.put("sort", "date");
		map.put("path", "clouds/explorer/date/");
		return "explorer";
	}

	@RequestMapping("explorer/popular/{index}/{max}")
	public String paginateByRating(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max, String order) {
		if (order == null) {
			order = "desc";
		}
		long count = exploredService.countExploreds(lang);
		List<ExplorerBean> explorerBeans = new ArrayList<ExplorerBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}
		int start = (int) ((index * max) - max);
		for (Explored ex : exploredService.getExploredsEntriesByRating(lang,
				start, max, order)) {
			explorerBeans.add(new ExplorerBean(ex));
		}
		map.put("explorerBeans", explorerBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("order", order);
		map.put("sort", "rating");
		map.put("path", "clouds/explorer/popular/");
		return "explorer";
	}

	@RequestMapping("explorer/byCateg/")
	public String paginateByCategorie(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang, Integer categ) {
		List<ExplorerBean> explorerBeans = new ArrayList<ExplorerBean>();
		Integer max = 16;
		Integer index = 1;
		int start = (int) ((index * max) - max);

		for (Explored ex : exploredService.getExploredsEntries(lang, start,
				max, categ)) {
			explorerBeans.add(new ExplorerBean(ex));
		}
		map.put("explorerBeans", explorerBeans);
		map.put("count", explorerBeans.size());
		return "tabListing";
	}

	@RequestMapping("explorer/category/{category}")
	public String paginateByCategory(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("category") Integer categ) {
		List<ExplorerBean> explorerBeans = new ArrayList<ExplorerBean>();
		Integer max = 16;
		Integer index = 1;
		int start = (int) ((index * max) - max);

		for (Explored ex : exploredService.getExploredsEntries(lang, start,
				max, categ)) {
			explorerBeans.add(new ExplorerBean(ex));
		}
		map.put("explorerBeans", explorerBeans);
		map.put("count", explorerBeans.size());
		return "filterExplorer";
	}

	@RequestMapping("explorer/dealson/{index}/{max}")
	public String paginateWithDeals(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max, String order) {
		// if (order == null) {
		order = "desc";
		// }
		long count = exploredService.countExploreds(lang);
		List<ExplorerBean> explorerBeans = new ArrayList<ExplorerBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}
		int start = (int) ((index * max) - max);
		for (Explored ex : exploredService.getExploredsEntriesWithDeals(lang,
				start, max, order)) {
			explorerBeans.add(new ExplorerBean(ex));
		}
		map.put("explorerBeans", explorerBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("order", order);
		map.put("sort", "dealson");

		map.put("path", "clouds/explorer/dealson/");
		return "explorer";
	}

	@RequestMapping("product")
	public String productDetail() {
		return "productDetail";
	}

}
